package com.lyx.server.controller;

import com.lyx.common.config.SystemConfig;
import com.lyx.common.param.*;
import com.lyx.common.vo.R;
import com.lyx.server.service.UserService;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;



/**
 * ⽤户表(User)表控制层
 *
 * @author McMeloxD
 * @since 2024-08-12 17:56:02
 */
@RestController
@RequestMapping("/server/user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService service;

    /**
     * 校验手机号
     */
    @GetMapping("checkphone")
    public R checkPhone(String phone){
        return service.checkPhone(phone);
    }

    /**
     * 发送验证码
     */
    @GetMapping("sendrcode")
    public R sendRcode(String phone){
        return service.sendRcode(phone);
    }

    /**
     * 校验验证码
     */
    @PostMapping("checkrcode")
    public R checkRcode(@RequestBody PhoneCode code){
        // 用requestbody将数据转为类对象
        return service.checkRcode(code);
    }

    /**
     * 用户注册
     */
    @PostMapping("register")
    public R register(@RequestBody UserAdd userAdd){
        return service.register(userAdd);
    }

    /**
     * 用户登录
     */
    @PostMapping("login")
    public R login(@RequestBody  @Validated UserLogin login, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return R.fail("参数不能为空!");
        }
        return service.login(login);
    }

    /**
     * 退出登录
     */
    @GetMapping("logout")
    public R logout(HttpServletRequest request) {
        return service.logout(request.getHeader(SystemConfig.HEADER_TOKEN));
    }


    /**
     * 查询全部用户
     */
    @GetMapping("all")
    public R All(){
        return service.queryAll();
    }


    /**
     * 找回发验证码*/
    @GetMapping("findPswSendRcode")
    public R findPswSendRcode(String phone){
        return service.findPswSendRcode(phone);
    }

    /**
     * 找回密码*/
    @PostMapping("findPassword")
    public R findPassword(@RequestBody UserFindPwd user){
        return service.findPassword(user);
    }


    /**
     * 修改用户信息
     */
    @PostMapping("updateinfo")
    public R updateInfo(@RequestBody UserUpdateInfo up){
        return service.updateInfo(up);
    }

    /**
     * 修改用户密码
     */
    @PostMapping("updatepassword")
    public R updatePassword(@RequestBody UserUpdatePsw uup){
        return service.updatePassword(uup);
    }

    /**
     * 签到
     */
    @GetMapping("signin")
    public R signIn(Integer uid){
        return service.signIn(uid);
    }
}

